package com.example.xianji.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Main_My_XuQiu_DetailsActivity extends Activity {
    /******************我发布的需求-需求详情****************************************/
    private RelativeLayout my_xuqiu_details_back;

    //    private SharedPreferences sp;
//    private Editor ed;
    private TextView text_xuqiu_bianhao, my_xuqiu_detail_title, my_xuqiu_details_status, my_xuqiu_details_tag, my_xuqiu_details_contant, my_xuqiu_details_time, my_xuqiu_details_money;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main__my__xu_qiu__details);
        my_xuqiu_details_back = (RelativeLayout) findViewById(R.id.my_xuqiu_details_back);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        text_xuqiu_bianhao = (TextView) findViewById(R.id.text_xuqiu_bianhao);
        my_xuqiu_detail_title = (TextView) findViewById(R.id.my_xuqiu_detail_title);
        my_xuqiu_details_status = (TextView) findViewById(R.id.my_xuqiu_details_status);
        my_xuqiu_details_tag = (TextView) findViewById(R.id.my_xuqiu_details_tag);
        my_xuqiu_details_contant = (TextView) findViewById(R.id.my_xuqiu_details_contant);
        my_xuqiu_details_time = (TextView) findViewById(R.id.my_xuqiu_details_time);
        my_xuqiu_details_money = (TextView) findViewById(R.id.my_xuqiu_details_money);
        my_xuqiu_details_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_My_XuQiu_DetailsActivity.this.finish();
            }
        });

        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_xuqiu_details();
            }
        });
    }

    public void get_xuqiu_details() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("id", getIntent().getIntExtra("my_xuqiu_detail_id", 0) + "");
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        httpUtils.send(HttpMethod.POST, Contant.my_xuqiu_details, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject response = new JSONObject(arg0.result);
                            if (response.getInt("status") == 1) {
                                JSONArray array = response.getJSONArray("data");
                                JSONObject obj = array.getJSONObject(0);
                                text_xuqiu_bianhao.setText(obj.getString("prono"));
                                my_xuqiu_detail_title.setText(obj.getString("title"));
                                if (obj.getInt("status") == 1 || obj.getInt("status") == 16 || obj.getInt("status") == 17 || obj.getInt("status") == 12) {
                                    my_xuqiu_details_status.setText("已发布");
                                } else if (obj.getInt("status") == 11) {
                                    my_xuqiu_details_status.setText("待审核");
                                } else if (obj.getInt("status") == 10) {
                                    my_xuqiu_details_status.setText("审核不通过");
                                } else if (obj.getInt("status") == 13 || obj.getInt("status") == 14) {
                                    my_xuqiu_details_status.setText("专家对接中");
                                }
                                my_xuqiu_details_tag.setText(obj.getString("name"));
                                my_xuqiu_details_contant.setText(obj.getString("details"));
                                if (obj.getString("end_at").equals("0000-00-00")) {
                                    my_xuqiu_details_time.setText("");
                                } else {
                                    my_xuqiu_details_time.setText(obj.getString("end_at"));
                                }
                                if (obj.getString("budget").equals("negotiable")) {
                                    my_xuqiu_details_money.setText("面议");
                                } else {
                                    my_xuqiu_details_money.setText(obj.getString("budget_val"));
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}